package org.ruiyichen.guizhou.agr.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;

import org.ruiyichen.guizhou.agr.R;
import org.ruiyichen.guizhou.agr.base.BaseFragment;
import org.ruiyichen.guizhou.agr.base.DataManger;
import org.ruiyichen.guizhou.agr.bean.Response;
import org.ruiyichen.guizhou.agr.config.Constant;
import org.ruiyichen.guizhou.agr.imp.IIrrigatedAreaFilter;
import org.ruiyichen.guizhou.agr.net.OkHttpManager;
import org.ruiyichen.guizhou.agr.ui.activity.HomeActivity;
import org.ruiyichen.guizhou.agr.ui.activity.ModifyPasswordActivity;
import org.ruiyichen.guizhou.agr.ui.mineview.BToast;
import org.ruiyichen.guizhou.agr.ui.mineview.RoundImageView;
import org.ruiyichen.guizhou.agr.utils.StrUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的
 */
public class MineFragment extends BaseFragment implements View.OnClickListener {

    RoundImageView rivMyHead;

    TextView tvMyName;

    @Override
    public void onResume() {
        super.onResume();
        if (!getUserVisibleHint()) {
            return;
        }
        initData();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (!hidden) {
            bHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    initData();
                }
            }, 500);
        }
        super.onHiddenChanged(hidden);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mRootView == null) {
            mRootView = inflater.inflate(R.layout.df_fragment_mine, container, false);
            bOkHttpManager = new OkHttpManager(getActivity(), bHandler);
            initViews();
            initEvents();
            initData();
        }
        //缓存的rootView需要判断是否已经被加过parent， 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) mRootView.getParent();
        if (parent != null) {
            parent.removeView(mRootView);
        }
        return mRootView;
    }

    private void initViews() {
        setTitle("个人中心").setRightImage(0).setRightText(null);
        rivMyHead = (RoundImageView) mRootView.findViewById(R.id.riv_my_head);
        tvMyName = (TextView) mRootView.findViewById(R.id.tv_my_name);
    }

    private void initEvents() {
        mRootView.findViewById(R.id.rl_modify_password).setOnClickListener(this);
        mRootView.findViewById(R.id.rl_my_vip).setOnClickListener(this);
        mRootView.findViewById(R.id.rl_settings).setOnClickListener(this);
        mRootView.findViewById(R.id.rl_login_out).setOnClickListener(this);
    }

    @Override
    public boolean onBackPressed() {
        return false;
    }

    @Override
    public void onClick(View view) {
        Intent it;
        switch (view.getId()) {
            case R.id.rl_modify_password:
                it = new Intent(getCustomActivity(), ModifyPasswordActivity.class);
                startActivity(it);
                break;
            case R.id.rl_my_vip:
                break;
            case R.id.rl_settings:
                break;
            case R.id.rl_login_out:
                //退出登录
                DataManger.getInstance().logout(getContext());
                break;
        }
    }

    private HomeActivity getCustomActivity() {
        return (HomeActivity) getActivity();
    }

    private void initData() {
        if (StrUtil.isNotEmpty(getCustomActivity().getIntent().getStringExtra("id"))) {
            Map<String, String> body = new HashMap<>();
            body.put("user_id", getCustomActivity().getIntent().getStringExtra("id"));
            body.put("token", DataManger.getInstance().getUserInfo().token);
            bOkHttpManager.httpRequest(Constant.RequestType.SEND_MESSAGE, Constant.Http.POST, body, false, new
                    TypeToken<Response<Void, ArrayList<? extends IIrrigatedAreaFilter>>>() {
                    }.getType(), true, false);
        }
    }

    @Override
    protected void onPostHandle(int requestType, Object response, boolean error, int errorCode, Object obj1, Object obj2) {
        super.onPostHandle(requestType, response, error, errorCode, obj1, obj2);
        if (error) {
            BToast.getInstance().show(getResources().getString(R.string.abnormal));
        } else {
            if (response == null) {
                BToast.getInstance().show(getResources().getString(R.string.datafailure));
            } else {
                if (requestType == Constant.RequestType.SEND_MESSAGE) {
                    Response<Void, List<? extends IIrrigatedAreaFilter>> result = (Response<Void, List<? extends IIrrigatedAreaFilter>>) response;
                    if (result.getCode() == 0) {
                    } else {
                        BToast.getInstance().show(result.getMsg());
                    }
                }
            }
        }
    }
}

